import { useEffect, useState } from 'react';
import qs from 'query-string'
import { useLocation } from 'react-router-dom';
// 路由嵌套时，跳转到子页面时，父路由query不要刷新
export const useQuery = () => {
    const { search, pathname } = useLocation();
    const [query, setQuery] = useState(qs.parse(search))
    const [currpath] = useState(pathname)
    const [currsearch] = useState(search)
    useEffect(() => {
        if (pathname === currpath && currsearch !== search) {
            setQuery(qs.parse(search))
        }
    }, [pathname, currpath, currsearch, search])
    return query
}